/**
 * Copyright (c) Jan 15, 2013 3:29:16 PM WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.action.face;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.extblog.wkm.web.action.ActionController;
import org.extblog.wkm.web.service.face.FaceService;
import org.extblog.wkm.web.vo.BodyItemFaceView;
import org.extblog.wkm.web.vo.HomePageFaceView;
import org.extblog.wkm.web.vo.LeftItemFaceView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author WKM
 * FaceController.java history
 * 1.WKM - Jan 15, 2013 - Create File
 */
@Controller
@RequestMapping("/home")
public class FaceController extends ActionController{
	
	/** 业务处理 **/
	private FaceService faceService = null;

	public FaceService getFaceService() {
		return faceService;
	}

	@Resource
	public void setFaceService(FaceService faceService) {
		this.faceService = faceService;
	}

	@RequestMapping("index.wkm")
	public ModelAndView index(){
		log.info("请求管理员编辑页面...");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("home/index");
		return mav;
	}
	
	@RequestMapping("children.wkm")
	public ModelAndView loadChild(){
		log.info("请求管理员编辑页面...");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("home/children");
		return mav;
	}
	
	@RequestMapping("bodyEdit.wkm")
	public ModelAndView bodyEdit(){
		log.info("请求管理员编辑页面...");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("home/editBody");
		return mav;
	}
	
	@RequestMapping("root.wkm")
	public ModelAndView editRoot(){
		log.info("请求编辑根节点页面...");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("home/editItem");
		return mav;
	}
	
	@RequestMapping("editHome.wkm")
	public ModelAndView editHome(){
		log.info("请求管理员编辑页面...");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("home/editHome");
		return mav;
	}

	@RequestMapping("home.wkm")
	public ModelAndView home(){
		log.info("请求左页面所有数据...");
		List<LeftItemFaceView> list = faceService.getLeftItems();
		log.info("请求左页面数据为:" + ArrayUtils.toString(list));
		ModelAndView mav = new ModelAndView();
		mav.addObject("list", list);
		mav.addObject("musics", faceService.getMusics());
		mav.addObject("homePage", faceService.getHomePage());
		mav.addObject("leftItemSize", list.size());
		mav.addObject("accordionId", 0);
		mav.addObject("selected", 0);
		mav.setViewName("home/home");
		return mav;
	}
	
	@RequestMapping("edit.wkm")
	public ModelAndView edit(){
		ModelAndView mav = new ModelAndView();
		log.info("获取根节点...");
		List<LeftItemFaceView> list = faceService.getLeftItems();
		try{
			mav.addObject("optionData", new ObjectMapper().writeValueAsString(createJsonDataForOption(list)));
			mav.addObject("optionSelected", new ObjectMapper().writeValueAsString(createJsonDataForSelected(list)));
		}catch (JsonGenerationException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 一般异常!");
		}catch (JsonMappingException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 映射异常!");
		}catch (IOException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 读写异常!");
		}
		mav.addObject("list",list);
		mav.addObject("messageForm0", "添加根节点信息!");
		log.info("去编辑页面...");
		mav.setViewName("home/faceedit");
		mav.addObject("tabIndex", "0");
		return mav;
	}
	
	@RequestMapping("edittable.wkm")
	public ModelAndView editTable(){
		log.info("编辑表格blog_items...");
		List<LeftItemFaceView> list = faceService.getLeftItems();
		ModelAndView mav = new ModelAndView();
		mav.addObject("list",list);
		mav.setViewName("home/editTable");
		return mav;
	}
	
	@RequestMapping("getUrlById.wkm")
	public ModelAndView getItemsUrls(@ModelAttribute("view") LeftItemFaceView view){
		log.info("请求Items url....");
		List<LeftItemFaceView> items = faceService.getLeftItems();
		log.info("请求左页面数据为:" + ArrayUtils.toString(items));
		ModelAndView mav = new ModelAndView();
		mav.addObject("list", items);
		mav.addObject("leftItemSize", items.size());
		mav.setViewName("home/bodys");
		List<Map<String,String>> list = faceService.loadBodys(view);
		
		List<String> names = new ArrayList<String>();
		LeftItemFaceView v = faceService.getLeftItems(view.getItemId());
		names.add(v.getItemName());
		mav.addObject("itemNames", names);
		mav.addObject("homePage", faceService.getHomePage());
		for(int index = 0;index < items.size();index++){
			LeftItemFaceView temp = items.get(index);
			if(temp.getItemId().equals(v.getItemParent())){
				mav.addObject("accordionId", index);
			}
		}
		if(!mav.getModel().containsKey("accordionId")){
			mav.addObject("accordionId", 0);
		}
		mav.addObject("selected", 1);
		mav.addObject("listUrls", list);
		return mav;
	}
	
	
	@RequestMapping("addroot.wkm")
	public ModelAndView addRootItems(@ModelAttribute("view") LeftItemFaceView view){
		log.info("添加根节点...");
		ModelAndView mav = new ModelAndView();
		String tabIndex = null;
		if(StringUtils.isBlank(view.getTabIndex())){
			tabIndex = "0";
		} else {
			tabIndex = view.getTabIndex();
		}
		
		if(StringUtils.isBlank(checked(view))) {
			if(faceService.addRootItem(view)){
				mav.addObject("messageForm" + tabIndex, "信息添加成功！");
			} else {
				mav.addObject("messageForm" + tabIndex, "信息添加失败！");
			}
		} else {
			mav.addObject("messageForm" + tabIndex, checked(view));
		}
		
		log.info("获取根节点...");
		List<LeftItemFaceView> list = faceService.getLeftItems();
		try{
			mav.addObject("optionData", new ObjectMapper().writeValueAsString(createJsonDataForOption(list)));
			mav.addObject("optionSelected", new ObjectMapper().writeValueAsString(createJsonDataForSelected(list)));
		}catch (JsonGenerationException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 一般异常!");
		}catch (JsonMappingException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 映射异常!");
		}catch (IOException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 读写异常!");
		}
		mav.addObject("list",list);
		
		mav.addObject("tabIndex", tabIndex);
		
		mav.setViewName("home/faceedit");
		
		return mav;
	}
	
	@RequestMapping("addbody.wkm")
	public ModelAndView addBodys(@ModelAttribute("view") BodyItemFaceView view){
		log.info("添加根节点...");
		ModelAndView mav = new ModelAndView();
		String tabIndex = null;
		if(StringUtils.isBlank(view.getTabIndex())){
			tabIndex = "0";
		} else {
			tabIndex = view.getTabIndex();
		}
		
		if(StringUtils.isBlank(checked(view))) {
			if(faceService.addBodys(view)){
				mav.addObject("messageForm" + tabIndex, "信息添加成功！");
			} else {
				mav.addObject("messageForm" + tabIndex, "信息添加失败！");
			}
		} else {
			mav.addObject("messageForm" + tabIndex, checked(view));
		}
		
		log.info("获取左边树节点...");
		List<LeftItemFaceView> list = faceService.getLeftItems();
		try{
			mav.addObject("optionData", new ObjectMapper().writeValueAsString(createJsonDataForOption(list)));
			mav.addObject("optionSelected", new ObjectMapper().writeValueAsString(createJsonDataForSelected(list)));
		}catch (JsonGenerationException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 一般异常!");
		}catch (JsonMappingException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 映射异常!");
		}catch (IOException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 读写异常!");
		}
		mav.addObject("list",list);
		
		mav.addObject("tabIndex", tabIndex);
		
		mav.setViewName("home/faceedit");
		
		return mav;
	}
	
	@RequestMapping("addhome.wkm")
	public ModelAndView addHome(@ModelAttribute("view") HomePageFaceView view){
		log.info("添加根节点...");
		ModelAndView mav = new ModelAndView();
		String tabIndex = null;
		if(StringUtils.isBlank(view.getTabIndex())){
			tabIndex = "0";
		} else {
			tabIndex = view.getTabIndex();
		}
		
		if(StringUtils.isBlank(checked(view))) {
			if(faceService.addHomePage(view)){
				mav.addObject("messageForm" + tabIndex, "信息添加成功！");
			} else {
				mav.addObject("messageForm" + tabIndex, "信息添加失败！");
			}
		} else {
			mav.addObject("messageForm" + tabIndex, checked(view));
		}
		
		log.info("获取左边树节点...");
		List<LeftItemFaceView> list = faceService.getLeftItems();
		try{
			mav.addObject("optionData", new ObjectMapper().writeValueAsString(createJsonDataForOption(list)));
			mav.addObject("optionSelected", new ObjectMapper().writeValueAsString(createJsonDataForSelected(list)));
		}catch (JsonGenerationException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 一般异常!");
		}catch (JsonMappingException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 映射异常!");
		}catch (IOException e) {
			// TODO: handle exception
			log.error("Map 转换 JSON 读写异常!");
		}
		mav.addObject("list",list);
		
		mav.addObject("tabIndex", tabIndex);
		
		mav.setViewName("home/faceedit");
		
		return mav;
	}
	
	/**
	 * 创建MAP TO JSON
	 * @param list
	 * @return
	 */
	private Map<String,Object> createJsonDataForOption(List<LeftItemFaceView> list){
		if(list == null || list.size() == 0){
			return null;
		}
		Map<String,Object> result = new HashMap<String, Object>();
		for(LeftItemFaceView view:list){
			if(view.getChildren() == null || view.getChildren().size() == 0){
				result.put(view.getItemName(), view.getItemId());
			} else {
				result.put(view.getItemName(), createJsonDataForOption(view.getChildren()));
			}
		}
		return result;
	}
	
	/**
	 * 创建默认选择项
	 * @param list
	 * @return
	 */
	private Map<String,Object> createJsonDataForSelected(List<LeftItemFaceView> list){
		if(list == null || list.size() == 0){
			return null;
		}
		Map<String,Object> result = new HashMap<String, Object>();
		
		for(LeftItemFaceView view:list){
			if(view.getChildren() == null || view.getChildren().size() == 0){
				result.put(view.getItemName(), view.getItemId());
			} else {
				result.put(view.getItemName(), createJsonDataForOption(view.getChildren()));
			}
			break;
		}
		return result;
	}
	
	private String checked(LeftItemFaceView view){
		if(StringUtils.isBlank(view.getItemParent())){
			return "父节点不能为空！";
		}
		if(StringUtils.isBlank(view.getItemName())){
			return "名称不能为空！";
		}
		if(!NumberUtils.isNumber(view.getItemType())){
			return "类型不合法！";
		}
		if(NumberUtils.toInt(view.getItemType()) > 1){
			return "类型不合法！";
		}
		if(NumberUtils.toInt(view.getItemType()) < 0){
			return "类型不合法！";
		}
		return "";
	}
	
	private String checked(BodyItemFaceView view){
		if(StringUtils.isBlank(view.getItemParent())){
			return "父节点不能为空！";
		}
		if(StringUtils.isBlank(view.getItemName())){
			return "名称不能为空！";
		}
		if(!NumberUtils.isNumber(view.getItemSeq())){
			return "顺序不能为空！";
		}
		if(StringUtils.isBlank(view.getTarget())){
			return "官方地址不能为空！";
		}
		if(StringUtils.isBlank(view.getItemDesc())){
			return "描述不能为空！";
		}
		return "";
	}
	
	private String checked(HomePageFaceView view){
		
		if(StringUtils.isBlank(view.getUrlPageHeight())){
			return "页面高度不能为空！";
		}
		if(!NumberUtils.isNumber(view.getUrlPageHeight())){
			return "页面高度必须为有效数字!";
		}
		if(StringUtils.isBlank(view.getUrlName())){
			return "名称不能为空！";
		}
		if(StringUtils.isBlank(view.getUrlAdd())){
			return "地址不能为空！";
		}
		if(StringUtils.isBlank(view.getUrlDesc())){
			return "描述不能为空！";
		}
		return "";
	}
}
